package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.dao.UserInvoiceMapper;
import mobi.hami.huapin.dao.UserOrderMapper;
import mobi.hami.huapin.dao.UserWechatMapper;
import mobi.hami.huapin.dto.UserOrderDTO;
import mobi.hami.huapin.model.UserInvoice;
import mobi.hami.huapin.model.UserWechat;
import mobi.hami.huapin.service.UserInvoiceService;
import mobi.hami.huapin.util.Dates;
import mobi.hami.huapin.util.Results;
import mobi.hami.huapin.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class UserInvoiceServiceImpl implements UserInvoiceService {

    private static final Logger logger = LoggerFactory.getLogger(UserInvoiceServiceImpl.class);

    @Autowired
    private UserInvoiceMapper userInvoiceMapper;

    @Autowired
    private UserWechatMapper userWechatMapper;

    @Autowired
    private UserOrderMapper userOrderMapper;

    public Result createUserInvoice(Map<String,Object> reqMap){

        /**
         * 验证订单id
         */
        Object order_id_obj = reqMap.get("order_id");
        if(null == order_id_obj || StringUtil.isEmpty(order_id_obj.toString())){
            return Results.failure("订单id为空");
        }
        Integer order_id = new Integer(order_id_obj.toString());

        /**
         * 验证user_id
         */
        Object user_id_obj = reqMap.get("user_id");
        if(null == user_id_obj || StringUtil.isEmpty(user_id_obj.toString())){
            return Results.failure("用户id为空");
        }
        Integer user_id = new Integer(user_id_obj.toString());

        /**
         * 验证openId
         */
        Object open_id_obj = reqMap.get("open_id");
        if(null == open_id_obj || StringUtil.isEmpty(open_id_obj.toString())){
            return Results.failure("open_id为空");
        }
        String openId = open_id_obj.toString();

        /**
         * 验证用户
         */
        UserWechat uwc = userWechatMapper.selectByWechatSmallId(openId);
        if(null == uwc){
            return Results.failure("该用户不存在");
        }
        if(!uwc.getUser_id().equals(user_id)){
            return Results.failure("用户信息不一致");
        }

        /**
         * 验证该用户的该订单(未开过发票,订单已收货状态,已收货时间后7天才能申请开发票)
         */
        UserOrderDTO userOrderDTO = userOrderMapper.getOneById(order_id);
        if(null == userOrderDTO){
            return  Results.failure("该订单不存在");
        }
        if(!userOrderDTO.getUser_id().equals(user_id)){
            return  Results.failure("用户信息不一致,暂不支持开发票");
        }
        if(userOrderDTO.getOrder_status() < 5){
            return  Results.failure("该订单未收货或签收,暂不支持开发票");
        }
        if(userOrderDTO.getOrder_status() >= 5){
            if(Dates.addHours(userOrderDTO.getUpdate_time(),7*24,null,null).before(new Date())){
                UserInvoice uic = userInvoiceMapper.find(user_id,openId,order_id);
                if(null != uic){
                    return  Results.failure("该订单已开发票");
                }
            }else {
                return  Results.failure("确认收货7天后才能申请开发票");
            }
        }

        /**
         * 验证发票类型
         */
        Object type_obj = reqMap.get("type");
        if(null == type_obj || StringUtil.isEmpty(type_obj.toString())){
            return Results.failure("发票类型为空");
        }
        Integer type = new Integer(type_obj.toString());

        /**
         * 验证税号  企业发票必须有税号
         */
        Object ein_obj = reqMap.get("ein");
        String ein = null;
        if(type == 1){
            if(null == ein_obj || StringUtil.isEmpty(ein_obj.toString())){
                return Results.failure("企业发票发票号必填");
            }
            ein = ein_obj.toString();
        }
        /**
         * 验证发票抬头
         */
        Object head_obj = reqMap.get("head");
        if(null == head_obj || StringUtil.isEmpty(head_obj.toString())){
            return Results.failure("发票内容为空");
        }
        String head = head_obj.toString();
        /**
         * 验证发票内容
         */
        Object content_obj = reqMap.get("content");
        if(null == content_obj || StringUtil.isEmpty(content_obj.toString())){
            return Results.failure("发票内容为空");
        }
        String content = content_obj.toString();

        /**
         * 验证金额>500才受理开票
         */
        Object money_obj = reqMap.get("money");
        if(null == money_obj || StringUtil.isEmpty(money_obj.toString())){
            return Results.failure("发票金额为空");
        }
        BigDecimal money = new BigDecimal(money_obj.toString());
        if(money.compareTo(new BigDecimal(500)) == -1){
            return Results.failure("发票金额大于500才受理开票");
        }

        /**
         * 验证收货人称谓
         */
        Object contact_name_obj = reqMap.get("contact_name");
        if(null == contact_name_obj || StringUtil.isEmpty(contact_name_obj.toString())){
            return Results.failure("收货人称谓为空");
        }
        String contact_name = contact_name_obj.toString();

        /**
         * 验证收货人收机号
         */
        Object contact_mobile_obj = reqMap.get("contact_mobile");
        if(null == contact_mobile_obj || StringUtil.isEmpty(contact_mobile_obj.toString())){
            return Results.failure("收货人称谓为空");
        }
        String contact_mobile = contact_mobile_obj.toString();

        /**
         * 验证收货人地址
         */
        Object contact_address_obj = reqMap.get("contact_address");
        if(null == contact_address_obj || StringUtil.isEmpty(contact_address_obj.toString())){
            return Results.failure("收货地址为空");
        }
        String contact_address = contact_mobile_obj.toString();

        UserInvoice ui = new UserInvoice(user_id,openId,order_id,type,ein,content,head,money,contact_name,contact_mobile,contact_address,new Date(),null,null,0);
        int i = userInvoiceMapper.insert(ui);
        if(i>0){
            return Results.success();
        }
        return Results.failure();
    }


    public Result existInvoice(Integer user_id,Integer order_id,String openId){
        UserInvoice uic = userInvoiceMapper.find(user_id,openId,order_id);
        if(null != uic){
            if(uic.getStatus().equals(0)){
                return Results.successWithData(0,"发票申请中");
            }
            if(uic.getStatus().equals(1)){
                return Results.successWithData(1,"已开且线下运送中");
            }
            if(uic.getStatus().equals(1)){
                return Results.successWithData(2,"用户已收,发票完成");
            }
        }
        return Results.successWithData(99,"未开过发票");
    }




}
